package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsProject;
import com.csun.dsms.model.domain.DsmsProjectCount;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.vo.ServiceRecordVo;
import com.csun.dsms.model.vo.project.DsmsProjectCountVo;
import com.csun.dsms.service.DsmsProjectCountService;
import com.csun.dsms.service.DsmsProjectService;
import com.csun.dsms.service.DsmsServiceRecordService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 11:53
 * @description: TODO
 */
@RestController
@RequestMapping("/project")
public class DsmsProjectController {

    @Resource
    private DsmsProjectService dsmsProjectServiceImpl;
    @Resource
    private DsmsServiceRecordService dsmsServiceRecordServiceimpl;
    @Resource
    private DsmsProjectCountService dsmsProjectCountServiceImpl;

    /*
    * 服务项目的添加
    * */
    @Authorization
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper projectAdd(@CurrentUser DsmsUser login, @RequestBody DsmsProject dsmsProject){
        dsmsProject.setCreator(login.getLoginName());
        dsmsProject.setCreatorId(login.getId());
        dsmsProject.setLastOperator(login.getLoginName());
        dsmsProject.setLastOperatorId(login.getId());
        DsmsProject project = new DsmsProject();
        project.setName(dsmsProject.getName());
        /*if(dsmsProject.getFrequency() > 99 || dsmsProject.getFrequency()<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"服务次数为1-99");
        }*/
        List<DsmsProject> query = dsmsProjectServiceImpl.queryByName(project);
        if(query.size()>0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"服务名已存在");
        }
        Integer i = dsmsProjectServiceImpl.save(dsmsProject);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 服务项目的删除
     * */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper projectDelete(@RequestBody DsmsProject dsmsProject){
        /*
         * 查询服务项目是否分配
         * */
        ServiceRecordVo serviceRecordVo = new ServiceRecordVo();
        serviceRecordVo.setProjectId(dsmsProject.getId());
        List<ServiceRecordVo> serviceRecordVos = dsmsServiceRecordServiceimpl.serviceProjectQuery(serviceRecordVo);
        if(serviceRecordVos.size()>0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该项目已分配特困人员");
        }
        Integer i = dsmsProjectServiceImpl.deleteByKey(dsmsProject);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 服务项目的修改
     * */
    @Authorization
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper projectUpdate(@CurrentUser DsmsUser login, @RequestBody DsmsProject dsmsProject){
        dsmsProject.setLastOperator(login.getLoginName());
        dsmsProject.setLastOperatorId(login.getId());
        dsmsProject.setUpdateTime(new Date());
        if(dsmsProject.getFrequency() != null && (dsmsProject.getFrequency() > 99 || dsmsProject.getFrequency()<1)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"服务次数为1-99");
        }
        /*
         * 查询服务项目在服务记录中是否待办
         * */
        ServiceRecordVo serviceRecordVo = new ServiceRecordVo();
        serviceRecordVo.setProjectId(dsmsProject.getId());
        List<ServiceRecordVo> serviceRecordVos = dsmsServiceRecordServiceimpl.serviceRecordQuery(serviceRecordVo);
        if(serviceRecordVos.size()>0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该项目正在使用");
        }
        List<DsmsProject> query = dsmsProjectServiceImpl.queryByName(dsmsProject);
        if(query.size()>0 && query.get(0).getId() != dsmsProject.getId()){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"服务名已存在");
        }
        Integer i = dsmsProjectServiceImpl.update(dsmsProject);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 服务项目的查询
     * */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public Wrapper projectQuery(@RequestBody DsmsProject dsmsProject){
        List<DsmsProject> dsmsProjects = dsmsProjectServiceImpl.query(dsmsProject);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(dsmsProjects));
    }

    /**
     * 添加每月服务次数
      * @param projectCountVo
     * @return null
     * @author HD
     * @date 2022/3/10 10:36
     */
    @Authorization
    @PostMapping("/addProjectCount")
    public Wrapper addProjectCount(@CurrentUser DsmsUser login, @RequestBody DsmsProjectCountVo projectCountVo){
        DsmsProjectCount dsmsProjectCount = new DsmsProjectCount();
        BeanUtils.copyProperties(projectCountVo,dsmsProjectCount);
        dsmsProjectCount.setUpdateInfo(login);
        List<DsmsProjectCount> dsmsProjectCounts = dsmsProjectCountServiceImpl.selectAll();
        //判断自理能力是否重复
        if(dsmsProjectCounts.size()>0){
            for (DsmsProjectCount projectCount:dsmsProjectCounts) {
                if(projectCount.getSelfCareAbility().equals(projectCountVo.getSelfCareAbility())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
                }
            }
        }
        Integer result = dsmsProjectCountServiceImpl.save(dsmsProjectCount);
        if(result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }

    /**
     * 查询每月服务次数
     * @param projectCountVo
     * @return null
     * @author HD
     * @date 2022/3/10 10:36
     */
    @Authorization
    @PostMapping("/queryProjectCount")
    public Wrapper queryProjectCount(@RequestBody DsmsProjectCountVo projectCountVo){
        List<DsmsProjectCountVo> projectCountVos = dsmsProjectCountServiceImpl.queryProjectCount(projectCountVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(projectCountVos));
    }

    /**
     * 删除每月服务次数
     * @param projectCountVo
     * @return null
     * @author HD
     * @date 2022/3/10 10:36
     */
    @Authorization
    @PostMapping("/deleteProjectCount")
    public Wrapper deleteProjectCount(@RequestBody DsmsProjectCountVo projectCountVo){
        Integer result = dsmsProjectCountServiceImpl.deleteByKey(projectCountVo.getId());
        if(result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }

    /**
     * 修改每月服务次数
     * @param projectCountVo
     * @return null
     * @author HD
     * @date 2022/3/10 10:36
     */
    @Authorization
    @PostMapping("/updateProjectCount")
    public Wrapper updateProjectCount(@RequestBody DsmsProjectCountVo projectCountVo){
        DsmsProjectCount projectCount = new DsmsProjectCount();
        //判断修改的自理程度是否已存在
        if(projectCountVo.getSelfCareAbility() != null){
            projectCount.setSelfCareAbility(projectCountVo.getSelfCareAbility());
            List<DsmsProjectCount> select = dsmsProjectCountServiceImpl.select(projectCount);
            if(select.size() > 0){
                for (DsmsProjectCount pc:select) {
                    if(pc.getSelfCareAbility().equals(projectCountVo.getSelfCareAbility())
                    && pc.getId() != projectCountVo.getId()){
                        return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
                    }
                }
            }
        }
        projectCount.setId(projectCountVo.getId());
        projectCount.setName(projectCountVo.getName());
        projectCount.setCount(projectCountVo.getCount());
        projectCount.setRemark(projectCountVo.getRemark());
        Integer result = dsmsProjectCountServiceImpl.update(projectCount);
        if(result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }

}
